<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>管理导航区域</title>
		<base href="<%=basePath%>">
		<link rel="stylesheet" href="css/common.css" type="text/css" />
		<link rel="stylesheet"
			href="<%=basePath%>/css/jquery-ui-1.8.5.custom.css" type="text/css" />
		<script src="<%=basePath%>js/jquery-1.5.js" language="javascript">
		</script>
		<script src="<%=basePath%>/js/pageInfo.js" type="text/javascript">
		</script>
		<script src="js/table.js" language="javascript">
</script>
		<script src="<%=basePath%>/js/jquery-ui-1.8.5.custom.min.js"
			language="javascript">
		</script>
		<script language="javascript">
$(function(){
		
			var isSpecialUser=${sessionScope.isSpecialUser};//特殊帐号，屏蔽增删改的功能
			
			//$("a[id^='changeState'],a[id^='deleteDorm']").attr("href",'javascript:alert("jj")');
			
			if(isSpecialUser){
				window.delUser=window.addUser=window.updateUser=function(){
					alert("Sorry!!该帐号权限不够，不能进行此操作")
				}
			}
})		
		

var userList;
var page = '<s:property value="pageJson" escape="false"/>';
page = eval('(' + page + ')');
var index_now = 1, size_now = page.pageSize;//当前设置的行数和页数
var userRoleColleges = '<s:property value="userRoleCollege" escape="false"/>';
var userRoles = '<s:property value="userRoles" escape="false"/>';

if (userRoleColleges == '')
	userRoleColleges = {};
if (userRoles == '')
	userRoles = {};
userRoleColleges = eval("(" + userRoleColleges + ")")
userRoles = eval("(" + userRoles + ")")

$(function() {
	$('#name').attr("value", "");
	$('#pass').attr("value", "");

	initPage(page.totalPage, page.pageIndex, page.pageSize, page.totalRec);
	ajaxSubmit(1, page.pageSize);
	/* 显示学院名称数据和用户类型数据(开始) */
	for ( var i = 0; i < userRoles.length; i++) {
		$('#utName').append(
				"<option value='" + userRoles[i].typeId + "'>"
						+ userRoles[i].typeName + "</option>");
	}
	$('#utName')
			.change(
					function() {
						$('#collegesName').empty();
						var colleges = userRoleColleges[$(this).val()];
						if (!colleges) {
							$('#collegesName').append(
									"<option value=''>整个学校</option>");
							return;
						}
						for ( var i = 0; i < colleges.length; i++) {
							$('#collegesName').append(
									"<option value='" + colleges[i].collegeId
											+ "'>" + colleges[i].collegesName
											+ "</option>");
						}
					})
	$('#utName').trigger("change")
	showList();
	/* 显示学院名称数据和用户类型数据(结束) */

//	$('#dataSubmit').unbind("click").click(addUser);
	/* 添加用户(结束) */
	/* 显示用户(开始) */
	function showList() {
		userList = '<s:property value="tuserJson" escape="false" />';
		if (userList == '') {
			userList = '{}';
		}
		userList = eval("(" + userList + ")");
		for ( var i = 0; i < userList.length; i++) {
			var tr;
			if (i % 2 != 0) {
				tr = '<tr style="background-color:#deefff" name="showUserMessage">';
			} else {
				tr = '<tr style="background-color:#ffffff" name="showUserMessage">';
			}
			var collegeName = ""
			if (!userList[i].colleges) {
				collegeName = "所有学院"
			} else {
				collegeName = userList[i].colleges.collegesName
			}
			tr += '<td align="center">' + (i + 1) + '</td>'
					+ '<td align="center">' + userList[i].userName + '</td>'
					+ '<td align="center">' + userList[i].userPassword
					+ '</td>' + '<td align="center">' + collegeName + '</td>'
					+ '<td align="center">' + userList[i].userType.typeName
					+ '</td>' + '<td align="center" class="upd">'
					+ '<div><a href="javascript:findUser(' + userList[i].userId
					+ ')">修改</a> &nbsp;&nbsp;&nbsp;&nbsp;'
					+ '<a href="javascript:delUser(' + userList[i].userId
					+ ')">删除</a>' + '</td></tr>';
			$('#showUser').append($(tr));
		}

	}
	/* 显示用户(结束) */

})

	/* 添加用户(开始) */
function addUser() {
		var tempName = $('#name').val();
		var tempPass = $('#pass').val();
		if (tempName == "") {
			alert("用户名不能为空!");
			return;
		}
		if (tempPass == "") {
			alert("密码不能为空!");
			return;
		} else {
			p = /^\w+$/
			if (!p.test(tempPass)) {
				alert("密码只能是字母或数字")
				return;
			}
		}
		$.post('json/addUserAction', {
			"userName" : tempName,
			"userPass" : tempPass,
			"collegeId" : $('#collegesName').val(),
			"userTypeId" : $('#utName').val(),
			"data" : new Date()
		}, function(data) {
			if (!eval(data.result)) {
				alert("增加失败！用户名已经存在或其他原因")
				return;
			}
			alert("增加成功")
			ajaxSubmit(index_now, size_now);
		});
	}
//清除数据
function clearData() {
	$("tr[name='showUserMessage']").remove();
}
//删除用户
function delUser(i) {
	if (confirm("确定要删除该用户吗?"))
		$.post('json/delUserAction', {
			"tuser.userId" : i
		}, function(data) {
			alert("删除成功");
			ajaxSubmit(index_now, size_now);
		})
}
//弹出修改用户窗口
function findUser(i) {
	$.post('json/tuserAction!findTuser', {
		"userId" : i,
		"data" : new Date()
	}, function(data) {
		var stu = eval("(" + data.userJson + ")");
		$("#username").attr("value", stu.userName);
		$("#password").attr("value", stu.userPassword);
		/* 显示学院名称数据和用户类型数据(开始) */
		$('#typeNameList').empty();
		for ( var i = 0; i < userRoles.length; i++) {
			$('#typeNameList').append(
					"<option value='" + userRoles[i].typeId + "'>"
							+ userRoles[i].typeName + "</option>");
		}
		$('#typeNameList').change(
				function() {
					$('#collegesList').empty();
					var colleges = userRoleColleges[$(this).val()];
					if (!colleges) {
						$('#collegesList').append(
								"<option value=''>整个学校</option>");
						return;
					}
					for ( var i = 0; i < colleges.length; i++) {
						$('#collegesList').append(
								"<option value='" + colleges[i].collegeId
										+ "'>" + colleges[i].collegesName
										+ "</option>");
					}
				})
		$('#typeNameList').trigger("change")

		$("#updateUserInfoDialog").show()
		//显示 div
			$("#updateUserInfoDialog").dialog( {
				modal : true,
				width : 450,
				buttons : {
					"确定" : function() {
						updateUser(stu.userId);
					},
					"取消" : function() {
						$("#studentInfoDialog").hide();
						$(this).dialog("close");
					}
				}
			})
		})
}
//修改用户信息
function updateUser(i) {
	var username = $("#username").val();
	var password = $("#password").val();
	if (username == "" || username == null) {
		alert("用户名不能为空!");
		return;
	}
	if (password == "" || password == null) {
		alert("密码不能为空!");
		return;
	} else {
		gre = /^\w+$/
		if (!gre.test(password)) {
			alert("密码只能是字母或数字")
			return;
		}
	}
	var collegeName = $('#collegesList').val();
	var typename = $('#typeNameList').val();
	$.post("json/updateUserAction", {
		"userId" : i,
		"tuser.userName" : username,
		"tuser.userPassword" : password,
		"collegeId" : collegeName,
		"userTypeId" : typename,
		"data" : new Date()
	}, function(data) {
		if (!eval(data.result)) {
			alert("更新失败！用户名已经存在或其他原因")
			$("#updateUserInfoDialog").hide()
			$("#updateUserInfoDialog").dialog("close");
			return;
		}
		alert("修改成功!");
		$("#updateUserInfoDialog").hide()
		$("#updateUserInfoDialog").dialog("close");
		ajaxSubmit(index_now, size_now);
	})
}

function ajaxSubmit(index, size) {
	index_now = index;
	size_now = size;
	$.post("json/tuserAction!showAll", {
		"pageInfo.pageIndex" : index,
		"pageInfo.pageSize" : size,
		"data" : new Date()
	}, function(data) {
		if (!data) {
			return;
		}
		var tempPage = eval('(' + data.pageJson + ')');
		showResult(data);
		initPage(tempPage.totalPage, tempPage.pageIndex, tempPage.pageSize,
				tempPage.totalRec);
	});
}
function showResult(data) {
	$('#name').attr("value", "");
	$('#pass').attr("value", "");
	clearData();
	var result = eval("(" + data.tuserJson + ")");
	for ( var i = 0; i < result.length; i++) {
		var tr2;
		if (i % 2 != 0) {
			tr2 = '<tr style="background-color:#deefff" name="showUserMessage">';
		} else {
			tr2 = '<tr style="background-color:#ffffff" name="showUserMessage">';
		}
		var collegeName = ""
		if (!result[i].colleges) {
			collegeName = "所有学院"
		} else {
			collegeName = result[i].colleges.collegesName
		}
		tr2 += '<td align="center">' + (i + 1) + '</td>'
				+ '<td align="center">' + result[i].userName + '</td>'
				+ '<td align="center">' + result[i].userPassword + '</td>'
				+ '<td align="center">' + collegeName + '</td>'
				+ '<td align="center">' + result[i].userType.typeName + '</td>'
				+ '<td align="center" class="upd">'
				+ '<div><a href="javascript:findUser(' + result[i].userId
				+ ')">修改</a> &nbsp;&nbsp;&nbsp;&nbsp;'
				+ '<a href="javascript:delUser(' + result[i].userId
				+ ')">删除</a>' + '</td></tr>';
		$('#showUser').append($(tr2));
	}
}
</script>
	</head>
	<body>
		<div class="adminhead">
			<jsp:include page="head.jsp"></jsp:include>
		</div>
		<div class="adminmain">
			<div class="left">
				<jsp:include page="leftMenu.jsp"></jsp:include>
			</div>
			<div class="right">
				<div id="nav">
					<ul>
						<li class="bg_image_onclick">
							用户管理
						</li>
					</ul>
				</div>
				<div id="sub_info">
					&nbsp;&nbsp;
					
					&nbsp;
					<span id="show_text">欢迎进入新生报告系统后台管理!</span>您现在正在：用户管理
				</div>

				<div id="man_zone">
					<!--************************************************************-->
					<table width="700" border="0" align="center" cellpadding="0"
						cellspacing="5" style="margin-top: 20px;">
						<tr>
							<td width="61" align="right">
								用户名：
							</td>
							<td width="186">
								<input id="name" type="text" size="16" />
							</td>
							<td width="72" align="right">
								密码：
							</td>
							<td width="168">
								<input id="pass" type="text" />
							</td>
							<td width="183" colspan="2" rowspan="2">
								<input id="dataSubmit" type="button"
									value="&nbsp;&nbsp;添&nbsp;加&nbsp;&nbsp;" onclick="addUser()"/>
							</td>
						</tr>
						<tr>
							<td align="right">
								身份：
							</td>
							<td>
								<select name="roleSelect" id="utName">
								</select>
							</td>
							<td align="right">
								学院：
							</td>
							<td>
								<select id="collegesName" name="collegeSelect">
								</select>
							</td>
						</tr>
					</table>
					<div id="msg" align="center" style="display: none">
						<font color='red'>添加成功!</font>
					</div>
					<!--************************************************************-->
					<table width="900" border="0" align="center" cellpadding="0"
						cellspacing="1"
						style="border: 1px solid #5cb0ff; margin-top: 20px;" class="t1"
						id="showUser">
						<tr style="color: #000; background-color: #90c9ff;">
							<th width="80" align="center">
								序号
							</th>
							<th width="145" align="center">
								用户名
							</th>
							<th width="115" align="center">
								密码
							</th>
							<th width="143" align="center">
								学院
							</th>
							<th width="207" align="center">
								身份
							</th>
							<th width="101" align="center">
								操作
							</th>
						</tr>
					</table>
					<!--上一页，下一页 开始-->
					<table width="900" class="page" border=0 align="center">
						<tr>
							<td align="left">
								<span id="AllPage">共几页/几条</span>
								<span id="setupNum">显示每页记录条数</span>
								<select id="setSize"
									onchange="ajaxChangeCount(this.options[this.options.selectedIndex].value)">
								</select>
							</td>
							<td align="right">
								<span id="Flexigrid_Bar"><a href="">上5页</a>&nbsp;<span
									class="dan">1</span>&nbsp;<a href="">2</a>&nbsp;<a href="">3</a>&nbsp;<a
									href="">下5页</a>&nbsp; </span>
								<span id="checkPage"><input type="text"
										style="width: 20px;">
										&nbsp;页<a href="">GO</a>
								</span>
							</td>
						</tr>
					</table>
					<!--上一页，下一页 结束-->

					<!--************************************************************-->
				</div>
				<div id="updateUserInfoDialog" title="修改用户信息" style="display: none;">
					<table width="100%" id="d1" border="0">
						<tr>
							<td align="right">
								用户名：
							</td>
							<td>
								<input id="username" type="text" size="16" value="" />
							</td>
						</tr>

						<tr>
							<td align="right">
								密码：
							</td>
							<td>
								<input id="password" type="text" size="16" value="" />
							</td>
						</tr>
						<tr>
							<td align="right">
								身份：
							</td>
							<td>
								<select id="typeNameList" name="roleSelect">
								</select>
							</td>
						</tr>
						<tr>
							<td align="right">
								学院：
							</td>
							<td>
								<select id="collegesList" name="collegeSelect">
								</select>
							</td>
						</tr>

					</table>
				</div>
			</div>
		</div>
		<div class="adminfooter">
			<jsp:include page="footer.jsp"></jsp:include>
		</div>

	</body>
</html>
